![](./icons/emoticons/forbidden.gif) |
This is an experimental feature. Feedback welcome ! |
![](./icons/emoticons/forbidden.gif) |
This validator will not work when its nested validator is a 'fieldexpression' validator due to 'fieldexpression' validator applies the expression directly on the action itself, not the value of current field. |
Description
Validate a property available in the object of a collection. This validator will not work
when the 'nested' validator is an 'fieldexpression' or 'expression' validator.. If we want to use
'fieldexpression' or 'expression' validator we might want to use eg. the following
expression for validation collections (using OGNL's projection and filtering capabilities.
For example if we have a collection of person s objects with 'name'
properties we might want to consider the following OGNL expression.
persons.{#this.name.length() > 4}.{? #this == false }.size() <= 0
The idea is
- - project across the collection of
person objects
and make sure the conditions are valid (eg. name is more than 4 characters
- - filter out the result of previous projection that are not valid (eg. false)
- - check the size of the filtered collection, if its greater than zero,
we have some validation that fails
Parameters
- property - the full property name that this validator should validate.
eg. if "persons" is a collection of Persons object with a property
called name the property would be "persons.name"
- validatorRef - validator name of an existing validator (could not be "collection" validator)
eg. required, requiredstring, int etc.
- validatorParams - the parameters to be passed into the validator referenced
by the "validatorParams" attribute above.
Examples
public class MyAction extends ActionSupport {
private List persons = new ArrayList();
....
public List getPersons() { return this.persons; }
public void setPersons(List persons) { this.persons = persons; }
}
public class Person {
private String name;
private Integer age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}
<validators>
<field name="persons">
<field-validator type="collection">
<param name="property">persons.name</param>
<param name="validatorRef">requiredstring</param>
<param name="validatorParams['defaultMessage']">Must be String</param>
<param name="validatorParams['trim']">true</param>
<message> ... </message>
</field-validator>
<field-validator type="collection">
<param name="property">persons.age</param>
<param name="validatorRef">required</param>
<param name="validatorParams['defaultMessage']">Must be filled in</param>
<message> ... </message>
</field-validator>
<field-validator type="collection">
<param name="property">persons.age</param>
<param name="validatorRef">int</param>
<param name="validatorParams['defaultMessage']">Needs to be an integer</param>
<message> ... </message>
</field-validator>
</field>
</validators>
|